//
//  DrawView.m
//  30201绘制图形
//
//  Created by student on 16/4/6.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "DrawView.h"

@implementation DrawView


// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
    [self drawGraphics1];
    [self drawGraphics2];
    [self drawGraphics3];
    [self drawGraphics4];
    [self drawGraphics5];
    [self drawGraphics6];
   //  [self drawGraphics7];
    [self drawGraphics8];
}


#pragma mark - 方法1: 在图形上下添加路径
-(void)drawGraphics1
{
    //1.得到图形上下文
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    
    //2.添加路径
    CGMutablePathRef path=CGPathCreateMutable();
    
    //设置起点
    CGPathMoveToPoint(path, NULL, 50, 50);
    
    //设置终点
    CGPathAddLineToPoint(path, NULL, 200, 200);
    
    //把路径添加到图形上下文中
    CGContextAddPath(ctx, path);
    
    //绘制出来（渲染）
    CGContextStrokePath(ctx);
}

#pragma mark - 方式2: 在图形上下文中添加路径(省略了创建路径)

-(void)drawGraphics2
{
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    
    CGContextMoveToPoint(ctx, 50, 50);
    
    CGContextAddLineToPoint(ctx, 150, 200);
    
    CGContextStrokePath(ctx);
}

#pragma mark - 方式3：UIKit里封装的绘图功能(OC类)
-(void)drawGraphics3
{
    //贝塞尔路径
    
    //创建路径
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(100, 200)];
    
    //绘制路径
    [path stroke];
}

#pragma mark - 方式二 绘制的一些设置
-(void)drawGraphics4
{
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 50, 50);
    CGContextAddLineToPoint(ctx, 100, 50);
    CGContextAddLineToPoint(ctx, 100, 100);
    
    //设置当前Stroke颜色
    [[UIColor redColor] setStroke];
    
    //线宽
    CGContextSetLineWidth(ctx, 10);
    //线接头地方的样式
    CGContextSetLineJoin(ctx, kCGLineJoinBevel);
    
    CGContextStrokePath(ctx);
}

#pragma mark - 方式三 绘制的一些设置
-(void)drawGraphics5
{
    UIBezierPath *path=[UIBezierPath bezierPath];
    [path moveToPoint:CGPointMake(50, 50)];
    [path addLineToPoint:CGPointMake(50, 100)];
    path.lineWidth=10;
    [[UIColor greenColor] setStroke];
    [path stroke];
    
    UIBezierPath *path2=[UIBezierPath bezierPath];
    [path2 moveToPoint:CGPointMake(50, 100)];
    [path2 addLineToPoint:CGPointMake(50, 150)];
    path2.lineWidth = 10;
    [[UIColor yellowColor] setStroke];
    [path2 stroke];


}

#pragma mark - 绘制曲线

//绘制QuadCurve曲线
-(void)drawGraphics6
{
    CGContextRef ctx=UIGraphicsGetCurrentContext();
    CGContextMoveToPoint(ctx, 50, 200);
    CGContextAddQuadCurveToPoint(ctx, 130, 250, 250, 200);
    [[UIColor redColor] setStroke];
    [[UIColor grayColor] setFill];
    CGContextStrokePath(ctx);//绘制线
    //CGContextFillPath(ctx);//填充
}

-(void)drawGraphics7
{
    //圆角矩形
    UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:CGRectMake(50, 50, 200,200) cornerRadius:100];
    [[UIColor yellowColor] setStroke];
    [[UIColor greenColor] setFill];
    path.lineWidth = 50;
    [path stroke];
    [path fill];

}

-(void)drawGraphics8{
    UIBezierPath *path = [UIBezierPath  bezierPath];
    
    [path moveToPoint:CGPointMake(125, 125)];
    
    // 中心点 半径 起始角度 终止角度 顺逆方向
    [path addArcWithCenter:CGPointMake(125, 125) radius:100 startAngle:0 endAngle:M_PI_2 clockwise:YES];
    
    [path closePath];
    [[UIColor whiteColor] setStroke];
    
    path.lineWidth = 5;
    
    [path stroke];
    [path fill];
    

}
@end
